package xyz.quanguanjia.masterboy;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import xyz.quanguanjia.masterboy.adapter.SettingListAdapter;
import xyz.quanguanjia.masterboy.decoration.HomeItemDecoration;
import xyz.quanguanjia.masterboy.me.MBDAboutActivity;
import xyz.quanguanjia.masterboy.model.MBDAccountInfoModel;

public class MBDSettingActivity extends AppCompatActivity implements HomeItemDecoration.HomeItemClickListener {

    private RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

        //导航栏
        Toolbar toolbar = findViewById(R.id.toolbar);
        MBDToolbarUtils.setCenterTitle(toolbar, "设置");
        MBDToolbarUtils.backButtonClicked(toolbar, new Runnable() {
            @Override
            public void run() {
                finish();
            }
        });

        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        this.recyclerView = findViewById(R.id.recyclerView_setting);

        // 设置上拉加载更多监听器
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                // 在这里判断是否到达列表底部，触发加载更多的操作
            }
        });

        LinearLayoutManager layoutManager = new LinearLayoutManager(MBDApplication.getContext());
        recyclerView.setLayoutManager(layoutManager);
        HomeItemDecoration hi = new HomeItemDecoration(this);
        recyclerView.addItemDecoration(hi);
        recyclerView.addItemDecoration(new DividerItemDecoration(MBDApplication.getContext(), LinearLayoutManager.VERTICAL));

        SettingListAdapter settingListAdapter = new SettingListAdapter();

        this.recyclerView.setAdapter(settingListAdapter);
    }

    @Override
    public void onItemClick(int position) {
        if (0 == position) {
            Intent intent = new Intent(MBDSettingActivity.this, MBDAboutActivity.class);
            startActivity(intent);
        } else if (1 == position) {
            MBDUserCenter.shared().setToken("");
            MBDAccountInfoModel.clear();

            Intent intent = new Intent(MBDNotificationCenter.NOTIFICATION_UPDATE_USERINFO);
            // 发送广播
            LocalBroadcastManager.getInstance(MBDApplication.getContext()).sendBroadcast(intent);

            Toast.makeText(this, "登出成功", Toast.LENGTH_SHORT).show();

            finish();
        } else if (2 == position) {

            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("警告")
                    .setMessage("注销账号会清空账号的所有信息，确定注销吗？")
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // 确定按钮点击事件
                            MBDSettingActivity.this.doWriteoffRequest();
                        }
                    })
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // 取消按钮点击事件
                        }
                    });

            AlertDialog dialog = builder.create();
            dialog.show();


        }

    }

    private void doWriteoffRequest() {
        MBDHttpClient.shared().doRequest(ServiceType.SERVICE_USER_WRITEOFF, null, new MBDHttpClient.Callback() {
            @Override
            public void completion(boolean success, Error error, MBDModelResponse response) {

                if (success) {
                    MBDUserCenter.shared().setToken("");
                    MBDAccountInfoModel.clear();

                    Intent intent = new Intent(MBDNotificationCenter.NOTIFICATION_UPDATE_USERINFO);
                    // 发送广播
                    LocalBroadcastManager.getInstance(MBDApplication.getContext()).sendBroadcast(intent);
                    finish();
                }


            }
        });
    }
}
